package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class SealSelect extends BaseService {
	

	public List<Map<String, Object>> selectSealAll(String srh_bank_id,String srh_name,String srh_num,Page page) {
		String sql = "select t.*,tb.name bname1,tb2.name bname2,tu.uname uname1,tu2.uname uname2,tm.mac,tm.mac_model,tstc.name cname,tstc.user_way,ts.title from t_seal t "
				+ " left join t_seal_new ts on t.status=0 and t.seal_new_id=ts._id "
				+ " left join t_bank tb on tb._id=t.bank_id and tb.status=0"
				+ " left join t_bank tb2 on tb2._id=t.bank_id and tb2.status=0"
				+ " left join t_mac tm on t.mac_id=tm._id  "
				+ " left join t_seal_type tst on ts.seal_type_id=tst._id and tst.status=0 "
				+ " left join t_seal_type_config tstc on ts.seal_type_config_id=tstc._id and tstc.status=0 "
				+ " left join t_user tu on tu._id=t.save_user_id  "
				+ " left join t_user tu2 on tu2._id=t.recieve_user_id  "
				+ " where t.status=0 and t.bank_id=? and t.name like ? and t.ture_code like ? ";
		List<String> params = new ArrayList<String>();
		if(ChkTools.isNull(srh_bank_id)){
			params.add(WebTools.getCurrentBankId());
		}else{
			params.add(srh_bank_id);
		}
		
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.name like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		
		if (ChkTools.isNull(srh_num)) {
			sql = sql.replace("and t.ture_code like ?", "");
		} else {
			params.add("%" + srh_num + "%");
		}
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(),page);
		return list;
	}
	
	public List<Map<String, Object>> selectSealUpDetail(String srh_bank_id) {
		String sql = "select t.*,ts.name,ts.true_code,tu.uname uname1,tu2.uname uname2,tb.name bname1,tb2.name bname2 from t_seal_over_log t "
				+ " left join t_seal ts on ts.status=0 and t.seal_id=ts._id "
				+ " left join t_user tu on tu._id=t.apply_user_id  "
				+ " left join t_user tu2 on tu2._id=t.verify_user_id  "
				+ " left join t_bank tb on tb._id=t.apply_bank_id and tb.status=0"
				+ " left join t_bank tb2 on tb2._id=t.verify_bank_id and tb2.status=0"
				+ " where t.status=0 and t.seal_id=? order by t.created ";
		List<String> params = new ArrayList<String>();
		if(ChkTools.isNull(srh_bank_id)){
			params.add(WebTools.getCurrentBankId());
		}else{
			params.add(srh_bank_id);
		}
		
		List<Map<String, Object>> list = jt.queryForList(sql,params.toArray());
		return list;
	}
	
	public List<Map<String, Object>> selectSealDownDetail(String srh_bank_id) {
		String sql = "select t.*,ts.name,ts.true_code,tu.uname uname1,tu2.uname uname2,tu3.uname uname3,tb.name bname1,tb2.name bname2 from t_seal_push t "
				+ " left join t_seal ts on ts.status=0 and t.seal_id=ts._id "
				+ " left join t_user tu on tu._id=t.user_id  "
				+ " left join t_user tu2 on tu2._id=t.rec_app_user_id  "
				+ " left join t_user tu3 on tu3._id=t.recieve_user_id  "
				+ " left join t_bank tb on tb._id=t.bank_id and tb.status=0"
				+ " left join t_bank tb2 on tb2._id=t.down_bank_id and tb2.status=0"
				+ " where t.status=0 and t.seal_id=? order by t.created ";
		List<String> params = new ArrayList<String>();
		if(ChkTools.isNull(srh_bank_id)){
			params.add(WebTools.getCurrentBankId());
		}else{
			params.add(srh_bank_id);
		}
		
		List<Map<String, Object>> list = jt.queryForList(sql,params.toArray());
		return list;
	}
}
